package com.fafa.pos.activity;

import android.support.v4.view.ViewCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.Spanned;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.Toast;
import com.fafa.pos.R;
import com.fafa.pos.dao.DBUtil;
import com.fafa.pos.dao.IConfigDao;
import com.fafa.pos.dao.impl.ConfigDaoImpl;
import com.fafa.pos.pojo.Config;
import com.fafa.pos.util.MyActivityManager;
import com.fafa.pos.util.MyToast;

public class QuickKeySetActivity extends TitleActivity {


    //dao层相关
    private DBUtil dbUtil;
    private IConfigDao configDao;

    //view相关
    private EditText setQuick1;
    private EditText setQuick2;
    private EditText setQuick3;
    private EditText setQuick4;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_quick_key_set);

        //加入activity管理
        MyActivityManager.getInstance().addActivity(this);


        //设置状态栏和标题栏同色
        Window window = this.getWindow();
        //取消设置透明状态栏,使 ContentView 内容不再覆盖状态栏
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

        //需要设置这个 flag 才能调用 setStatusBarColor 来设置状态栏颜色
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        //设置状态栏颜色
        window.setStatusBarColor(getResources().getColor(R.color.LoginBlue));

        ViewGroup mContentView = (ViewGroup) this.findViewById(Window.ID_ANDROID_CONTENT);
        View mChildView = mContentView.getChildAt(0);
        if (mChildView != null) {
            //注意不是设置 ContentView 的 FitsSystemWindows, 而是设置 ContentView 的第一个子 View . 预留出系统 View 的空间.
            ViewCompat.setFitsSystemWindows(mChildView, true);
        }

        setTitle("快捷键设置");
        showBackwardView(R.string.text_back, true);
        showForwardView(R.string.text_save, true);

        initDao();
        initView();
        initEvent();
    }

    private void initDao() {
        dbUtil = new DBUtil(getApplicationContext().getFilesDir().getAbsoluteFile() + "/pos.db");
        configDao = new ConfigDaoImpl(dbUtil);
    }

    private void initView() {
        setQuick1 = (EditText) findViewById(R.id.setQuick1);
        setQuick2 = (EditText) findViewById(R.id.setQuick2);
        setQuick3 = (EditText) findViewById(R.id.setQuick3);
        setQuick4 = (EditText) findViewById(R.id.setQuick4);



        Config config = configDao.getConfig();
        if(config.getProperties().get("quickKey1")!=null){
            setQuick1.setText(config.getProperties().get("quickKey1").trim());
        }
        if(config.getProperties().get("quickKey2")!=null){
            setQuick2.setText(config.getProperties().get("quickKey2").trim());
        }
        if(config.getProperties().get("quickKey3")!=null){
            setQuick3.setText(config.getProperties().get("quickKey3").trim());
        }
        if(config.getProperties().get("quickKey4")!=null){
            setQuick4.setText(config.getProperties().get("quickKey4").trim());
        }

        setQuick1.setSelection(setQuick1.getText().toString().length());
        setQuick2.setSelection(setQuick2.getText().toString().length());
        setQuick3.setSelection(setQuick3.getText().toString().length());
        setQuick4.setSelection(setQuick4.getText().toString().length());


        setQuick1.setFilters(new InputFilter[]{new InputFilter() {
            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                if(source.equals(".") && dest.toString().length() == 0){
                    return "0.";
                }
                if(dest.toString().contains(".")){
                    int index = dest.toString().indexOf(".");
                    int mlength = dest.toString().substring(index).length();
                    if(mlength == 3){
                        return "";
                    }
                }
                return null;
            }
        }});
        setQuick2.setFilters(new InputFilter[]{new InputFilter() {
            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                if(source.equals(".") && dest.toString().length() == 0){
                    return "0.";
                }
                if(dest.toString().contains(".")){
                    int index = dest.toString().indexOf(".");
                    int mlength = dest.toString().substring(index).length();
                    if(mlength == 3){
                        return "";
                    }
                }
                return null;
            }
        }});
        setQuick3.setFilters(new InputFilter[]{new InputFilter() {
            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                if(source.equals(".") && dest.toString().length() == 0){
                    return "0.";
                }
                if(dest.toString().contains(".")){
                    int index = dest.toString().indexOf(".");
                    int mlength = dest.toString().substring(index).length();
                    if(mlength == 3){
                        return "";
                    }
                }
                return null;
            }
        }});
        setQuick4.setFilters(new InputFilter[]{new InputFilter() {
            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                if(source.equals(".") && dest.toString().length() == 0){
                    return "0.";
                }
                if(dest.toString().contains(".")){
                    int index = dest.toString().indexOf(".");
                    int mlength = dest.toString().substring(index).length();
                    if(mlength == 3){
                        return "";
                    }
                }
                return null;
            }
        }});


    }

    private void initEvent() {

    }

    @Override
    protected void onForward(View forwardView) {
//        super.onForward(forwardView);
        if ("".equals(setQuick1.getText().toString().trim()) || "".equals(setQuick2.getText().toString().trim()) || "".equals(setQuick3.getText().toString().trim()) || "".equals(setQuick4.getText().toString().trim())) {

            Toast toast = Toast.makeText(QuickKeySetActivity.this,"快捷输入键不能为空",Toast.LENGTH_SHORT);
            MyToast.showMyToast(toast,1000);
        }else{
            Config config = configDao.getConfig();
            config.getProperties().put("quickKey1",setQuick1.getText().toString().trim());
            config.getProperties().put("quickKey2",setQuick2.getText().toString().trim());
            config.getProperties().put("quickKey3",setQuick3.getText().toString().trim());
            config.getProperties().put("quickKey4",setQuick4.getText().toString().trim());
            configDao.saveConfig(config);
            finish();
        }
    }
}
